package com.dazzle.cameralib;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Rect;
import android.os.IBinder;
import android.util.Size;

import com.dazzle.cameralib.utils.DebugUtils;

/**
 * Created by gavin on 2016/11/24.
 */

public class CameraRecordManager {

    private static final String TAG =  "CameraRecordManager";

    private Context mContext;
    private ICameraRecoderInterface mCameraRecordBinder = null;

    public CameraRecordManager(Context context) {
        mContext =context;
    }

    /**
     * 初始化，绑定remote服务
     */
    public void init() {
        bindCameraRecoredService();
    }

    /**
     * 释放资源，注销remote服务
     */
    public void release() {
        unbindCameraRecoredService();
    }

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            DebugUtils.LogE(TAG, "Bind CameraRecordService");
            mCameraRecordBinder = ICameraRecoderInterface.Stub.asInterface(service);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };


    private void bindCameraRecoredService(){
        startCameraServer();
        try{
            final Intent intent = new Intent(mContext, CameraRecordService.class);
            if (mCameraRecordBinder == null) {
                mContext.bindService(intent, conn, Service.BIND_AUTO_CREATE);
            }
        }catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    private void unbindCameraRecoredService() {
        try{
            mContext.unbindService(conn);
            mCameraRecordBinder = null;
        }catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 打开摄像头服务
     */
    public void startCameraServer() {
        try{
            final Intent intent = new Intent(mContext, CameraRecordService.class);
            mContext.startService(intent);
        }catch (NullPointerException e) {
            e.printStackTrace();
        }

    }

    /**
     * 关闭摄像头，且关闭服务
     */
    public void stopCameraServer() {
        try{
            mCameraRecordBinder = null;
            final Intent intent = new Intent(mContext, CameraRecordService.class);
            mContext.stopService(intent);
        }catch (NullPointerException e) {
            e.printStackTrace();
        }

    }

    /**
     * 打开/关闭摄像预览， 录像时不能关闭预览，只能隐藏
     * @param enable true  - 打开
     *                false - 关闭
     * @return true - success
     *          false - fail
     */
    public boolean enablePreview(boolean enable) {
        try {
            mCameraRecordBinder.enablePreview(enable);
        } catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 打开/关闭录像并且打开预览
     * @param enable true  - 打开
     *                false - 关闭
     * @return true - success
     *          false - fail
     */
    public boolean enableRecord(boolean enable) {
        try {
            mCameraRecordBinder.enableRecord(enable);
        } catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 返回录像状态
     * @return 录像状态
     */
    public boolean isRecording() {
        try {
            return mCameraRecordBinder.isRecoreding();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 返回预览状态
     * @return 预览状态
     */
    public boolean isPreviewEnable() {
        try {
            return mCameraRecordBinder.isPreviewEnable();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 设置摄像头分辨率
     * @return w - 宽， h - 高
     */
    public void setVideoSize(int w, int h) {
        try {
            mCameraRecordBinder.doSetVideoSize(w, h);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置预览窗口位置大小
     * @param rect 坐标
     */
    public void setPreviewWindowRect(Rect rect) {
        try {
            mCameraRecordBinder.doSetWindowRect(rect.left, rect.top, rect.right, rect.bottom);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取预览窗口是否隐藏
     * @return true - 隐藏
     *          false - 显示
     */
    public boolean isPreviewHide() {
        try {
            return mCameraRecordBinder.isPreviewHide();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 隐藏预览窗口
     */
    public void hidePreviewWindow() {
        try {
            mCameraRecordBinder.hidePreviewWindow();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示预览窗口
     */
    public void showPreviewWindow() {
        try {
            mCameraRecordBinder.showPreviewWindow();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}
